/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
/**
* Basic field class
* $Log: FieldMessage.java,v $
* Revision 1.1.1.1  2004/04/22 12:56:14  travisb
* Fir
*
* Revision 1.1.1.1  2003/12/21 09:04:08  jaspervdb
* no message
*
* Revision 1.1.1.1  2003/10/20 15:30:48  travisb
* First import into CVS at Cellstop
*
* Revision 1.1.1.1  2003/03/30 15:25:28  tbulford
* no message
*
* Revision 1.1  2003/01/12 08:30:34  travis
* Import x MANY
*
* Revision 1.1.1.1  2002/12/16 22:14:01  jasper
* no message
*
* Revision 1.3  2002/05/02 14:36:06  JasperV
* Commiter: NPPS Pins finished except for loading of MTN and CellC Pins
*
* Revision 1.2  2002/04/22 09:03:37  travisb
* Before Backup
*
* Revision 1.1  2002/02/20 13:30:43  JasperV
* First time add to new Applications layer
*
* Revision 1.1  2002/01/24 14:18:14  travisb
* First Version of the Application Layer
*
* Revision 1.1  2002/01/21 08:06:07  travisb
* First version commited to CVS
*
*/

package blueprint4j.messages;

import java.io.*;
import java.util.*;
import java.text.*;

public class FieldMessage extends Field {
	Message field_message;

	/**
	* Defines the field
	* @param p_fieldsize_type
	* The way the size of the field is stored in 8583 must be one of the
	* following LENGTH_FIXED,LENGTH_LVAR,LENGTH_LLVAR or LENGTH_LLLVAR
	* @param p_fieldsize
	* this is the maximun size allows or the fixed length. Fields must
	* not exceed this size even if they are of a type that has a size header
	*/
	FieldMessage( int p_fieldsize_type, int p_fieldsize, int p_field_position, String _name, Message _field_message )
	throws ExceptionMessage {
		super(p_fieldsize_type, p_fieldsize, p_field_position, _name);
		field_message = _field_message;
	}

	protected int getLength()
	throws ExceptionMessage {
		return getBytes().length;
	}

	void set(byte buf[])
	throws ExceptionMessage {
		setBinary(buf);
	}

	byte[] getPadded( int length )
	throws ExceptionMessage {
		byte[] bytes=getBytes();
		if (length==bytes.length) return bytes;
		byte[] buf=new byte[length];
		for (int t=0;t<length;t++){
			if (t<bytes.length){
				buf[t]=bytes[t];
			}else{
				buf[t]=(byte)0;
			}
		}
		return buf;
	}

	public boolean isNull()
	throws ExceptionMessage {
		return field_message==null;
	}

	public byte[] getBytes() throws ExceptionMessage {
		byte[] b = null;
		try {
			ByteArrayOutputStream buffer = new ByteArrayOutputStream();
			field_message.write(buffer);
			b = buffer.toByteArray();
			buffer.close();
		} catch (IOException ioe) {
			throw new ExceptionMessage("IOException " + ioe.getMessage());
		}
		return b;
	}

	void setBinary( byte p_value[] )
	throws ExceptionMessage {
		try {
			ByteArrayInputStream buffer = new ByteArrayInputStream(p_value);
			field_message.read(buffer);
			buffer.close();
		} catch (IOException ioe) {
			throw new ExceptionMessage("IOException " + ioe.getMessage());
		}
	}

	void verify( String p_value )
	throws ExceptionMessage {
	}
}

